package cn.com.callback

import cn.com.elite.EliteBean
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.text.DateFormat
import java.text.SimpleDateFormat

/**
 *
 * @Title: autoHSTStateUpdate
 * @Description:华盛证券 修复chat_request 内涉及异常请求的数据--内部接口
 * @remak：g.网聊chat_request   表的   request_status_id 字段记录 是7  为客户放弃。 但是实际这通会话接入人工了 且发送了有效对话   -- 赵杰 don  需要在客户点取消排队的时候判断会话是否生成
 * request_status_id 字段记录 是4  是 异常掉线
 * @author: luo
 * @date 2023-01-31 12:32:00
 * ***************************************代码合并   赵杰2023-02-01发布生产，详情见 aipHSTchatRequestUp ***************************8
 *
 */

public class autoHSTStateUpdate implements CallBackInvoke{
    private static final Logger log = LoggerFactory.getLogger(autoHSTStateUpdate.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String paramString) {
        DateFormat df = new SimpleDateFormat("HH:mm");
        Date date = new Date();
        String NowTime = df.format(date);

        try {


            String getSwitchSQL = "select cr.chat_session_id,cr.id from chat_request cr \n" +
                    "left join  chat_message cm on cr.chat_session_id=cm.chat_session_id\n" +
                    "where  cm.chat_session_id is not null and (cr.request_status_id=4 or cr.request_status_id=7)\n" +
                    "GROUP BY cr.chat_session_id,cr.id\n" +
                    "ORDER BY 1 desc";
            List getSwitchSQLResult = eliteBean.chaxun_sql(getSwitchSQL, "dataSource");
            log.debug("异常数据：" + getSwitchSQLResult.toString());
            log.debug("异常条数：" + getSwitchSQLResult.size());

            int c=0;
            if (getSwitchSQLResult.size() > 0) {
                for (int i = 0; i < getSwitchSQLResult.size(); i++) {

                    Map getSwitchMap = (Map) getSwitchSQLResult.get(i);

                    String Session_Id = getSwitchMap.get("chat_session_id");
                    String Id = getSwitchMap.get("id");
                    if (Session_Id) {

                        String UpSql = "update chat_request set request_status_id=1 where id=" + Id + "  and chat_session_id=" + Session_Id;

                        c= eliteBean.update(UpSql, "dataSource")
                    }

                    log.debug("修复条数：" + c++);
                }

            }
        } catch (Exception e) {
            log.error("状态修改定时任务操作异常", e);
        }

    }


}

